package com.example.exercise.user.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.user.vo.AppUserVO;
import com.example.exercise.user.dao.entity.AppUserEntity;
import com.example.exercise.user.dao.AppUserDAO;

/**
* 
*/
@Service
public class AppUserService {

    @Autowired
    private AppUserDAO appUserDAO;

    public List<AppUserVO> findByWhere(Page<?> page, AppUserVO appUserVO){
        List<AppUserVO> list = new ArrayList<>();
        List<AppUserEntity> list0 = appUserDAO.findByWhere(page, appUserVO);
        for (AppUserEntity appUserEntity : list0) {
            list.add(toAppUserVO(appUserEntity));
        }
        return list;
    }

    public AppUserVO findByKey(AppUserVO appUserVO){
        AppUserEntity appUserEntity = appUserDAO.findByKey(appUserVO);
        return toAppUserVO(appUserEntity);
    }

    public int insert(AppUserVO appUserVO){
        return appUserDAO.insert(toAppUserEntity(appUserVO));
    }

    public int updateByKey(AppUserVO appUserVO){
        return appUserDAO.updateByKey(toAppUserEntity(appUserVO));
    }

    public int deleteByKey(AppUserVO appUserVO){
        return appUserDAO.deleteByKey(appUserVO);
    }

    private AppUserVO toAppUserVO(AppUserEntity appUserEntity) {
        AppUserVO appUserVO = new AppUserVO();
                appUserVO.setUserId(String.valueOf(appUserEntity.getUserId()));
        appUserVO.setUserName(appUserEntity.getUserName());
        appUserVO.setPassword(appUserEntity.getPassword());
        appUserVO.setUseState(String.valueOf(appUserEntity.getUseState()));
        appUserVO.setIfAdmin(String.valueOf(appUserEntity.getIfAdmin()));
        appUserVO.setEmail(appUserEntity.getEmail());
        appUserVO.setMobile(appUserEntity.getMobile());
        appUserVO.setPersonDesc(appUserEntity.getPersonDesc());

        return appUserVO;
    }

    private AppUserEntity toAppUserEntity(AppUserVO appUserVO) {
        AppUserEntity appUserEntity = new AppUserEntity();
                String userId = appUserVO.getUserId();
        if(userId != null) {
            appUserEntity.setUserId(Integer.parseInt(appUserVO.getUserId()));
        }
        appUserEntity.setUserName(appUserVO.getUserName());
        appUserEntity.setPassword(appUserVO.getPassword());
        String useState = appUserVO.getUseState();
        if(useState != null) {
            appUserEntity.setUseState(Integer.parseInt(appUserVO.getUseState()));
        }
        String ifAdmin = appUserVO.getIfAdmin();
        if(ifAdmin != null) {
            appUserEntity.setIfAdmin(Integer.parseInt(appUserVO.getIfAdmin()));
        }
        appUserEntity.setEmail(appUserVO.getEmail());
        appUserEntity.setMobile(appUserVO.getMobile());
        appUserEntity.setPersonDesc(appUserVO.getPersonDesc());

        return appUserEntity;
    }

}